package com.deyuanyun.pic.ctrl.controller;

import com.deyuanyun.pic.common.dto.PageJsonBean;
import com.deyuanyun.pic.common.util.StringUtils;
import com.deyuanyun.pic.common.util.ajax.AjaxSupport;
import com.deyuanyun.pic.ctrl.controller.dto.ExinspTaskExpandDTO;
import com.deyuanyun.pic.ctrl.controller.vo.ExinspTaskUpdateOrCreateVO;
import com.deyuanyun.pic.ctrl.controller.vo.ExinspTaskVO;
import com.deyuanyun.pic.ctrl.service.ExternalInspProjectService;
import com.deyuanyun.pic.settings.controller.BaseController;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.MessageFormat;

/**
 *  外检项目controller
 *  @author axiao(xj08.dream@gmail.com)
 *  @date 2016-09-22
 *  @version 1.0
 *  Histroy: [修改人][时间][描述]
 */
@Controller
@RequestMapping("/corrctrl/external")
public class ExternalInspProjectController extends BaseController {

    @Autowired ExternalInspProjectService externalInspProjectService;

    /**
     * 跳转到外检项目管理页面
     * @param
     * @return
     * @author axiao(xj08.dream@gmail.com)
     * @date 2016-09-23
     */
    @RequestMapping("/projectManagement.html")
    public String toProjectManagement() {
        return "/corrctrl/external/projectManagement";
    }

    /**
     * 查询外检项目列表
     * @param exinspTaskVO
     * @author axiao(xj08.dream@gmail.com)
     * @date 2016-09-22
     */
    @RequestMapping("/queryExinspTasks.json")
    @ResponseBody
    public void queryExinspTasks(ExinspTaskVO exinspTaskVO) {
        PageJsonBean jsonBean = new PageJsonBean();
        jsonBean.setList(externalInspProjectService.queryExinspTasks(exinspTaskVO));
        AjaxSupport.sendSuccessText("success", StringUtils.DATE, jsonBean);
    }

    /**
     * 删除，批量删除为导入的外检项目数据(为逻辑删除)
     * @param ids
     * @author axiao(xj08.dream@gmail.com)
     * @date 2016-09-23
     */
    @RequestMapping("/deleteExinspTasks.json")
    @ResponseBody
    public void deleteExinspTasks(String[] ids) {
        String str = "已选择{0}条项目，成功删除{1}条项目。";
        if(ids == null) {
            AjaxSupport.sendFailText("fail", MessageFormat.format(str, 0, 0));
            return;
        }
        int n = externalInspProjectService.updateDeleted(ids);
        AjaxSupport.sendSuccessText(Integer.toString(n), MessageFormat.format(str, ids.length, n));
    }
    
    /**
     * 跳转到新建外检项目或者编辑外检项目页面
     * @param id
     * @return 
     * @author axiao(xj08.dream@gmail.com)
     * @date 2016-09-23
     */
    @RequestMapping("/createOrUpdate.html")
    public String createOrUpdate(String id, ModelMap modelMap) {
        if(StringUtils.isNotEmpty(id)) {
            modelMap.addAttribute("exinspTask", externalInspProjectService.getInfoById(id));
        }
        return "/corrctrl/external/projectManagementAdd";
    }

    /**
     * 新建外检项目或者编辑外检项目ajax
     * @param taskUpdateOrCreateVO
     * @return
     * @author axiao(xj08.dream@gmail.com)
     * @date 2016-09-23
     */
    @RequestMapping("/createOrUpdate.json")
    @ResponseBody
    public void createOrUpdate(ExinspTaskUpdateOrCreateVO taskUpdateOrCreateVO) {
        ExinspTaskExpandDTO dto = new ExinspTaskExpandDTO();
        BeanUtils.copyProperties(taskUpdateOrCreateVO, dto);
        externalInspProjectService.createOrUpdate(dto);
        AjaxSupport.sendSuccessText("success", "");
    }

}
